package com.qiangqiang.task.server.mapper.provider;


import java.lang.reflect.Field;
import java.time.Instant;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * @author : sky
 * @date : 2018/7/12
 * @description :
 */
public class BaseProvider {
    protected final static String DOT = ",";
    protected final static long NOW_TIME = Instant.now().toEpochMilli();

    /**
     * @Description: 驼峰格式字符串转换为下划线格式字符串
     * @Param:
     * @return:
     * @Author: sky.lin
     * @Date: 2019/2/9
     */
    protected String camelToUnderline(String param) {
        if (param == null || "".equals(param.trim()))
            return "";
        // 处理首字母为大写的情况
        if (Character.isUpperCase(param.charAt(0)))
            param = String.valueOf(param.charAt(0)).toLowerCase() + param.substring(1);
        int len = param.length();
        StringBuffer sb = new StringBuffer(len);
        for (int i = 0; i < len; i++) {
            char c = param.charAt(i);
            if (Character.isUpperCase(c)) {
                sb.append("_");
                sb.append(Character.toLowerCase(c));
            } else
                sb.append(c);
        }
        return sb.toString();
    }

    /**
     * @Description: 下划线格式字符串转换为驼峰格式字符串
     * @Param:
     * @return:
     * @Author: sky.lin
     * @Date: 2019/2/9
     */
    protected String underlineToCamel(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        int len = param.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = param.charAt(i);
            if (c == '_' && (++i < len))
                sb.append(Character.toUpperCase(param.charAt(i)));
            else
                sb.append(c);
        }
        return sb.toString();
    }

    /**
     * @Description: 生成插入的SQL语句
     * @Param:
     * @return:
     * @Author: sky.lin
     * @Date: 2019/2/9
     */
    protected String generateInsertSQL(Class<?> c) {
        StringBuffer sb = new StringBuffer("insert into ");
        String table = camelToUnderline(c.getSimpleName());
        sb.append(table).append("(");
        Field[] fields = c.getDeclaredFields();
        sb.append(String.join(",", Arrays.stream(fields).
                map(e -> camelToUnderline(e.getName()))
                .collect(Collectors.toList())));
        sb.append(") values(");
        sb.append(String.join(",", Arrays.stream(fields).
                map(e -> "#{" + underlineToCamel(table) + "." + e.getName() + "}").
                collect(Collectors.toList())));
        sb.append(")");
        return sb.toString();
    }

    /**
     * @Description: 分页工具类
     * @Param: page: 页码  pageSize: 页数
     * @return:
     * @Author: sky.lin
     * @Date: 2019/2/9
     */
    protected String getLimit(int page, int pageSize) {
        StringBuffer sb = new StringBuffer();
        if (page > 0 && pageSize > 0) {
            sb.append("limit ").append((page - 1) * pageSize).append(DOT + pageSize);
        }
        return sb.toString();
    }
}
